<style>
    /* 产品 */
    .g-product-list {
        padding: 10px;
    }

    .g-product {
        border-radius: 3px;
        margin-bottom: 10px;
		box-shadow: 0 0 5px rgba(0, 0, 0, .2);
        display: flex;
        padding: 10px;
        position: relative;
    }

    .g-product-pic {
        width: 240upx;
        height: 240upx;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-right: 10px;
    }
	.g-product-pic image{
		width: 240upx;
		height: 240upx;
		overflow: hidden;
	}

    .g-product-info {
        flex: 1;
        position: relative;
    }

    .g-price {
        font-size: 26upx;
        margin-bottom: 5px;
		display: flex;
		align-items: center;
		justify-content: space-between;
    }

    .g-price text.red {
		color: #d71311;
        font-size: 40upx;
    }

    .small-text {
        color: #828080;
    }

    .u-product-buy {
        color: #D71511;
        border-radius: 8upx;
        padding: 4upx 14upx;
        font-size: 28upx;
        display: inline-block;
		border: 1px solid #D71511;
		margin-right: 20upx;
    }
    .u-product-sign {
        width: 75upx;
		height: 75upx;
		overflow: hidden;
        position: absolute;
        top: 5px;
        left: 5px;
    }

    .u-product-name {
		height: 82upx;
		line-height: 41upx;
    }
    .u-order {
        font-size: 24upx;
        color: #D71511;
        border: 1px solid #D71511;
        border-radius: 2px;
        padding: 0 2px;
		margin-right: 8upx;
    }
	.m-full-loading image{
		width: 140upx;
		height: 140upx;
	}
    /* 弹框 */
    .u-popup-mask {
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        background: rgba(0, 0, 0, .5);
        z-index: 9;
    }

    .g-popup {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 10;
        border-radius: 5px;
        background: #fff;
        text-align: center;
        width: 80%;
    }

    .u-popup-title {
        padding: 15px 0;
        box-shadow: 0 0 10px rgba(0, 0, 0, .5);
        font-size: 32upx;
    }
	.u-popup-cont {
		padding-top: 40upx;
		font-size: 36upx;
		text-align: center;
	}
    .u-popup-close {
        position: absolute;
        top: 10px;
        right: 10px;
    }

    .u-popup-buy {
        color: #fff;
        background: #d71311;
        border-radius: 4px;
        width: 40%;
        padding: 10px 0;
		margin: 60upx auto;
        box-shadow: 0 7px 13px -2px rgba(255,0,0,.5);
    }
</style>

<template>
    <view class="page">
       
        <view class="g-product-list">
            <view class="g-product" v-for="(item, index) in data" :key="index" v-if="isGoodsTpye">
                <navigator open-type="navigate" class="g-product-pic" :url="'/pages/user/group_order?team_id='+item.team_id">
                    <image :src="item.thumb" mode="widthFix"></image>
                </navigator>
                <view class="g-product-info">
                    <navigator open-type="navigate" class="u-product-name f-two-ellipsis" :url="'/pages/user/group_order?team_id='+item.team_id"><text class="u-order">第{{item.no}}期</text>{{item.goods_name}}</navigator>
                    <view class="g-price"><view>￥<text class="red">{{item.shop_price}}</text></view<text>本次已拼{{item.buy_num}}</text></view>
                    <view class="small-text">公布时间&nbsp;&nbsp;{{item.open_time}}</view>
					<button class="u-product-buy" @tap="handle(item.team_id)" v-if="item.is_lottery == 1&&item.has_lottery_handle.handle_result == 0">委托代售</button>
					<button class="u-product-buy" @tap="returnGoods(item.team_id)" v-if="item.is_lottery == 0&&item.has_lottery_handle.handle_result == 0">退货</button>
					<button class="u-product-buy" @tap="open" :data-id="item.team_id">发货</button>
                    
                </view>
                <image class="u-product-sign" :src="item.promotion_id == 1 ? '../../static/icons/ydt/product8.png' : '../../static/icons/ydt/product_free.png'"
                    mode="widthFix"></image>
            </view>
			<view class="g-product" v-for="(item, index) in data" :key="index" v-if="!isGoodsTpye">
			    <navigator open-type="navigate" class="g-product-pic" url="#">
			        <image :src="item.thumb" mode="widthFix"></image>
			    </navigator>
			    <view class="g-product-info">
			        <navigator open-type="navigate" class="u-product-name f-two-ellipsis" url="#">{{item.goods_name}}</navigator>
			        <view class="g-price"><view>￥<text class="red">{{item.shop_price}}</text></view></view>
			        <view class="small-text">时间&nbsp;&nbsp;{{item.time}}</view>
			        <button class="u-product-buy" :data-id="item.team_id">{{item.status}}</button>
			    </view>
			</view>
            
        </view>
		<!-- 弹框蒙版 -->
		<view class="u-popup-mask" v-if="bPopup" @touchmove.stop=""></view>
		<view class="g-popup" v-if="bPopup" @touchmove.stop="">
		    <view class="u-popup-title">提示</view>
		    <view class="u-popup-cont">委托代售{{weiNum}}</view>
			<button class="u-popup-buy" @tap="toPay">确认</button>
		    <view class="u-popup-close" @tap="closePopup">
		        <close size="50" color="#847c7c"></close>
		    </view>
		</view>
        <view class="m-no-data" v-if="showNodata">
            <image src="../../static/images/no_order.png" mode="widthFix"></image>
        </view>
		<view class="m-full-loading" v-if="showloading">
		    <image src="../../static/images/loading.gif"></image>
		</view>
    </view>
</template>

<script>
    import henglang from '@/common/common.js';
    export default {
        data() {
            return {
                data: [], // 商品列表数据
                sendData: {  // 请求商品列表发送的数据
                    goods_name: '',
                    page: 1,
                },
				totalPages:1,//总页数
                showloading: false,
                showNodata: false,
				isGoodsTpye: henglang.isGoodsTpye(),
				idx: 0,
				team_id:0,
				isResult: false,
				bPopup: false,
				weiNum:'',
				weiList:{
					team_id:'',
					id:''
				}
            };
        },
        methods: {
            back() {
                uni.navigateTo({
                	url:'/pages/user/user_index'
                })
            },
            open(e) { // 打开弹框
                let that = this;
                let id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url:'/pages/user/group_handle?team_id=' + id
				})
            },
			closePopup() {
				this.bPopup = !this.bPopup;
			},
			toPay() {
				let that = this
				that.showloading = true
				henglang.post('order/lotteryHandle',
				{team_id:that.weiList.team_id,
				 data:[{id:that.weiList.id,action:1}]
				 },true,function(res){
					that.showloading = false
					that.bPopup = false
					if(res.data.code == 0){
						henglang.showToast(res.data.msg,500)
						setTimeout(()=>{
							uni.redirectTo({
								url:'/pages/user/group_finish'
							})
						},500)
					}else{
						henglang.showToast(res.data.msg)
					}
				});		
			},
			returnGoods(team_id){
				let that = this
				that.data.forEach((item,index)=>{
					if (item.team_id == team_id) {
						that.weiList.team_id = team_id
						that.weiList.id = 0
					}
				})	
				uni.showModal({
					title: '退货',
					content: '确认退货？',
					success: function (res) {
						if (res.confirm) {
							that.toPay();
						}
					}
				})
			},
            handle(team_id) {
				let that = this
				that.bPopup = true
				that.data.forEach((item,index)=>{
					if (item.team_id == team_id) {
						let list = item.has_lottery_handle
						let price = (Number(list.shop_price) * list.back_discount)/100
						let id = list.id
						that.weiNum = price.toFixed(2)
						that.weiList.team_id = team_id
						that.weiList.id = id
					}
				})
			},
            request(type) { // 请求数据
				let that = this;
				that.showloading = true;
				if(!henglang.checkLogin()){
					that.showloading = false;
					that.showNodata = true;
					return false;
				}
				let url = '';
				let data = {};
				if(that.isGoodsTpye){
					url = 'order/getHasTeam'
					data = that.sendData
				}else{
					url = 'order/demiorder'
					data = {type:2}
				}
				if(that.sendData.page <= that.totalPages){
					henglang.get(url,data,true,function(res){
						that.showloading = false;
						if (!res.data.data.length) {
							that.showNodata = true;
							return false;
						}
						if (type == 'plus') {
							that.data = that.data.concat(res.data.data);
						} else {
							that.data = res.data.data;
							that.totalPages = res.data.total_pages;
						}
					});			
				}else{
					that.showloading = false;
				}
            },
            
        },
		onLoad({isResult,team_id}) {
			this.request();
		},
        onReachBottom() {
			this.sendData.page += 1;
            this.request('plus');
        },
		onBackPress(options) {  
		    if (options.from === 'navigateBack') {  
		        return false;  
		    }  
		    this.back();  
		    return true;  
		}
    }
</script>
